﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Sunny.UI;

namespace MMS
{
    public partial class Page_AddGoods : UIPage
    {
        public Page_AddGoods()
        {
            InitializeComponent();
        }
        db_mmsEntities conn = new db_mmsEntities();
        private void Page_AddGoods_Load(object sender, EventArgs e)
        {
            uiDataGridView2.SetRowHeight(30);
        }

        private void btn_AddOld_Click(object sender, EventArgs e)
        {
            string gosNb = txt_GosNb.Text;
            int gossum =  Convert.ToInt32(txt_GosSum.Text);
            tb_goods gos = conn.tb_goods.ToList().FirstOrDefault(r => r.goodsID == gosNb);
            if (gos == null)
            {
                MessageBox.Show("未查询到该商品");
                return;
            }
            int oldnum = Convert.ToInt32(gos.goodsSum);
            gos.goodsSum = oldnum + gossum;
            conn.SaveChanges();
            MessageBox.Show(gos.goodsName + "成功添加" + gossum + "件");
        }

        private void btn_AddNew_Click(object sender, EventArgs e)
        {
            for(int i = 0; i < uiDataGridView2.Rows.Count; i++)
            {
                if (uiDataGridView2.Rows[i].Cells[0].Value == null)
                {
                    continue;
                }
                string gosid = uiDataGridView2.Rows[i].Cells[0].Value.ToString();
                var isgos = conn.tb_goods.FirstOrDefault(r => r.goodsID ==gosid );
                if (isgos != null)
                {
                    MessageBox.Show("商品"+gosid+"已存在！");
                    continue;
                }
                tb_goods gos = new tb_goods();
                gos.goodsID =gosid;
                gos.goodsName= uiDataGridView2.Rows[i].Cells[1].Value.ToString();
                gos.goodsSum= Convert.ToInt32(uiDataGridView2.Rows[i].Cells[2].Value);
                gos.goodsParam= uiDataGridView2.Rows[i].Cells[3].Value.ToString();
                gos.goodsUnits = uiDataGridView2.Rows[i].Cells[4].Value.ToString();
                gos.goodsType = uiDataGridView2.Rows[i].Cells[5].Value.ToString();
                gos.putPrice = uiDataGridView2.Rows[i].Cells[6].Value.ToString();
                gos.getPrice = uiDataGridView2.Rows[i].Cells[7].Value.ToString();
                gos.isShow = 1;
                conn.tb_goods.Add(gos);
                conn.SaveChanges();
            }
            MessageBox.Show("商品添加成功！");
            uiDataGridView2.Rows.Clear();
        }
    }
}
